import { Component, OnInit, Input, forwardRef, Output, EventEmitter } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { OnChanges } from '@angular/core/src/metadata/lifecycle_hooks';
import { SmallDirective } from 'src/app/directive/small.directive';

@Component({
  selector: 'min-input',
  templateUrl: './input.component.html',
  styleUrls: ['./input.component.less'],
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => InputComponent),
    multi: true
  }]
})
export class InputComponent implements OnInit,OnChanges {
  constructor() { }
  
  @Input() placeholder:string = "please input";
  @Input() icon:string = "";
  @Input() title:string = "";
  @Input() type:string = "";
  
  ngOnInit() {
  }
  ngOnChanges(changes:any){
    if(changes.icon && changes.icon.currentValue){
      this.icon = changes.icon.currentValue;
    }  
    if(changes.title && changes.title.currentValue){
      this.title = changes.title.currentValue;
    }   
    if(changes.type && changes.type.currentValue){
      this.type = changes.type.currentValue;
    }
  }
  public input:string;
  onModelChange: Function = () => { };
  onModelTouched: Function = () => { };
  change(data){
    this.onModelChange(data)
  }
  writeValue(currentValue: any) {
    this.input = currentValue;
  }
  registerOnChange(fn: Function): void {
    this.onModelChange = fn;
  }

  registerOnTouched(fn: Function): void {
    this.onModelTouched = fn;
  }

}
